<?php
namespace Admin\Controller;
//轮播图配置

use Common\Controller\AdminbaseController;

class SysbannerController extends AdminbaseController{

	protected $sys_model;
	protected $kind_model;

	public function _initialize() {
		parent::_initialize();
		$this->sys_model = D("Common/SysBanner");
        $this->kind_model = D("Common/SysBannerKind");
	}

	// 轮播图列表
	public function index(){
	    echo 123;
		/**搜索条件**/
		$kind_name = I('request.kind_name');
		if($kind_name){
			$where['kind_name'] = array('like',"%$kind_name%");
		}

		$count=$this->sys_model->where($where)->count();
		$page = $this->page($count, 10);
        $list = $this->sys_model
            ->where($where)
            ->order("id DESC")
            ->limit($page->firstRow, $page->listRows)
            ->select();
		$this->assign("page", $page->show('Admin'));
        $this->assign('list',$list);
		$this->display();
	}

	// 轮播图添加
	public function add(){
        $kind = $this->kind_model->field('kind_id,kind_name')->select();
        $this->assign('kind',$kind);
		$this->display();
	}

	// 轮播图添加提交
	public function add_post(){
		if(IS_POST){
            if ($this->sys_model->create() !== false) {
                $date = $_POST;
                if (!$_FILES) {
                    $this->error("图片不能为空！");die;
                }
                $imgUrl = $this->upload();
                if (!$imgUrl) {
                    $this->error("添加失败！");die;
                }
                $date['banner_img_url'] = $imgUrl;
                /*当前管理员name*/
                $date['create_user'] = get_current_admin_name();
                $date['create_time'] = get_admin_time();
                $result = $this->sys_model->add($date);
                if ($result !== false) {
                    $this->success("添加成功！", U("Sysbanner/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->sys_model->getError());
            }

		}
	}

	// 轮播图编辑
	public function edit(){
	    $id = I('get.id',0,'intval');
		$sys = $this->sys_model->where(array('id' => $id))->find();
        $kind = $this->kind_model->field('kind_id,kind_name')->select();
        $this->assign('kind',$kind);
		$this->assign('sys',$sys);
		$this->display();
	}

	// 轮播图编辑提交
	public function edit_post(){
		if (IS_POST) {
            if ($this->sys_model->create() !== false) {
                $date = $_POST;
                if ($_FILES['banner_img_url']['name']) {
                    $imgUrl = $this->upload();
                    if (!$imgUrl) {
                        $this->error("编辑失败！");
                    }
                    $date['banner_img_url'] = $imgUrl;
                }else{
                    $date['banner_img_url'] = $date['image_url'];
                }
                /*当前管理员name*/
                $date['update_user'] = get_current_admin_name();
                $date['update_time'] = get_admin_time();
                $result = $this->sys_model->save($date);
                if ($result !== false) {
                    if ($_FILES['banner_img_url']['name']) {
                        unlink('./'.C("UPLOADPATH").$date['image_url']);
                    }
                    $this->success("编辑成功！", U("Sysbanner/index"));
                } else {
                    $this->error("编辑失败！");
                    unlink('./'.C("UPLOADPATH").$date['banner_img_url']);
                }
            } else {
                $this->error($this->sys_model->getError());
            }

		}
	}

	// 轮播图删除
	public function delete(){
	    $id = I('get.id',0,'intval');
        $sys = $this->sys_model->where(array('id' => $id))->find();
        $imgurl = $sys['banner_img_url'];
		if ($this->sys_model->delete($id)!==false) {
            unlink('./'.C("UPLOADPATH").$imgurl);
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}

    // 图片上传
    public function upload(){
        $config=array(
            'rootPath' => './'.C("UPLOADPATH"),
            'savePath' => '',
            'maxSize' => 2097152,//500K
            'saveName'   =>    array('uniqid',''),
            'exts'       =>    array('jpg', 'png', 'jpeg'),
            'autoSub'    =>    false,
        );
        $upload = new \Think\Upload($config,'Local');//先在本地裁剪
        $info=$upload->upload();
        //开始上传
        if ($info) {
            //上传成功
            //写入附件数据库信息
            $first=array_shift($info);
            $file=$first['savename'];
            return $file;
        } else {
            return flase;
        }
    }

}